package com.ktgj.customer.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.customer.domain.vo.ScheduleDetailsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.customer.domain.TravelItinerary;
import com.ktgj.customer.service.ITravelItineraryService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 旅游行程Controller
 * 
 * @author ruoyi
 * @date 2021-11-10
 */
@ControllerAdvice
@RestController
@RequestMapping("/travelItinerary")
public class TravelItineraryController extends BaseController
{
    @Autowired
    private ITravelItineraryService travelItineraryService;

    /**
     * 查询旅游行程列表
     */
    //@PreAuthorize(hasPermi = "customer:itinerary:list")
    @GetMapping("/list")
    public TableDataInfo list(TravelItinerary travelItinerary)
    {
        startPage();
        List<TravelItinerary> list = travelItineraryService.selectTravelItineraryList(travelItinerary);
        return getDataTable(list);
    }

    /**
     * 导出旅游行程列表
     */
    //@PreAuthorize(hasPermi = "customer:itinerary:export")
    @Log(title = "旅游行程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TravelItinerary travelItinerary) throws IOException
    {
        List<TravelItinerary> list = travelItineraryService.selectTravelItineraryList(travelItinerary);
        ExcelUtil<TravelItinerary> util = new ExcelUtil<TravelItinerary>(TravelItinerary.class);
        util.exportExcel(response, list, "旅游行程数据");
    }

    /**
     * 获取旅游行程详细信息
     */
    //@PreAuthorize(hasPermi = "customer:itinerary:query")
    @GetMapping(value = "/{travelItineraryId}")
    public AjaxResult getInfo(@PathVariable("travelItineraryId") Long travelItineraryId)
    {
        return AjaxResult.success(travelItineraryService.selectTravelItineraryById(travelItineraryId));
    }

    /**
     * 新增旅游行程
     */
    //@PreAuthorize(hasPermi = "customer:itinerary:add")
    @Log(title = "旅游行程", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TravelItinerary travelItinerary)
    {
        return toAjax(travelItineraryService.insertTravelItinerary(travelItinerary));
    }

    /**
     * 修改旅游行程
     */
    //@PreAuthorize(hasPermi = "customer:itinerary:edit")
    @Log(title = "旅游行程", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TravelItinerary travelItinerary)
    {
        return toAjax(travelItineraryService.updateTravelItinerary(travelItinerary));
    }

    /**
     * 删除旅游行程
     */
    //@PreAuthorize(hasPermi = "customer:itinerary:remove")
    @Log(title = "旅游行程", businessType = BusinessType.DELETE)
	@DeleteMapping("/{travelItineraryIds}")
    public AjaxResult remove(@PathVariable Long[] travelItineraryIds)
    {
        return toAjax(travelItineraryService.deleteTravelItineraryByIds(travelItineraryIds));
    }

    /**
     * 查询指定旅游产品行程安排
     * */
    @PostMapping("/scheduleDetailsVoList")
    public AjaxResult scheduleDetailsVoList(@RequestBody Map<String, Object> map) {
        if (map.get("groupId") != null && !map.get("groupId").equals("")){
            Long groupId = Long.valueOf(map.get("groupId").toString());
            List<ScheduleDetailsVo> scheduleDetailsVos;
            try {
                scheduleDetailsVos = travelItineraryService.scheduleDetailsVoList(groupId);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }
            if (scheduleDetailsVos != null && scheduleDetailsVos.size() > 0){
                return AjaxResult.success(scheduleDetailsVos);
            }
            else {
                return AjaxResult.error("未查到数据");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }
}
